<?php

namespace backend\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\LogoAdv;

/**
 * LogoAdvSearch represents the model behind the search form about `common\models\LogoAdv`.
 */
class LogoAdvSearch extends LogoAdv
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['logo_adv_id', 'pic', 'tag_display', 'status'], 'integer'],
            [['adv_name', 'tag', 'url', 'recommend_starttime', 'recommend_endtime', 'add_user', 'add_time'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = LogoAdv::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'sort'=> ['defaultOrder' => ['logo_adv_id'=>SORT_DESC]] 
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'logo_adv_id' => $this->logo_adv_id,
            'pic' => $this->pic,
            'tag_display' => $this->tag_display,
            'status' => $this->status,
            'add_time' => $this->add_time,
        ]);

        $query->andFilterWhere(['like', 'adv_name', $this->adv_name]);
        if(!empty($this->recommend_starttime) && !empty($this->recommend_endtime))
        {
            $query->andFilterWhere(['between', 'recommend_starttime', $this->recommend_starttime, $this->recommend_endtime])
                  ->orFilterWhere(['between', 'recommend_endtime', $this->recommend_starttime, $this->recommend_endtime]);
        }
//            ->andFilterWhere(['like', 'tag', $this->tag])
//            ->andFilterWhere(['like', 'url', $this->url])
//            ->andFilterWhere(['like', 'add_user', $this->add_user]);

        return $dataProvider;
    }
}
